<template>
<div class="container">
  <el-form ref="form" :model="form" label-width="80px">

    <el-row type="flex">
      <el-col :span="12">
        <el-form-item label="货品名称">
          <el-input v-model="form.goodsName"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="存储单位">
          <el-select v-model="form.unit" placeholder="请选择">
            <el-option
                v-for="item in units"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row type="flex">
      <el-col :span="12">
        <el-form-item label="货品重量">
          <el-input-number v-model="form.weight" controls-position="right"  :min="1" :max="999999" :step="0.1"></el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="重量单位">
          <el-select v-model="form.weightUnit" placeholder="请选择">
            <el-option
                v-for="item in weights"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row type="flex">
      <el-col :span="8">
        <el-form-item label="成本价">
          <el-input-number v-model="form.goodsCostPrice" controls-position="right"  :min="1" :max="999999" :step="0.1"></el-input-number>
        </el-form-item>

      </el-col>
      <el-col :span="8">

        <el-form-item label="销售价">
          <el-input-number v-model="form.goodsSalesPrice" controls-position="right"  :min="1" :max="999999" :step="0.1"></el-input-number>
        </el-form-item>
      </el-col>

      <el-col :span="8">
        <el-form-item label="供应商">
          <el-select v-model="form.supplier" placeholder="请选择">
            <el-option
                v-for="item in supplierList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>


    
     <el-row type="flex">
       <el-col :span="8">
         <el-form-item label="货品类型">
           <el-select v-model="form.goodsType" placeholder="请选择">
             <el-option
                 v-for="item in goodsTypeList"
                 :key="item.value"
                 :label="item.label"
                 :value="item.value">
             </el-option>
           </el-select>
         </el-form-item>
       </el-col>
       <el-col :span="8">
         <el-form-item label="是否启用">
           <el-switch v-model="form.status"></el-switch>
         </el-form-item>
       </el-col>
<!--       状态-->
       <el-col :span="8">
         <el-form-item label="销售方式">
           <el-radio v-model="form.salesType" label="0">按件</el-radio>
           <el-radio v-model="form.salesType" label="1">按重量</el-radio>
         </el-form-item>
       </el-col>
<!--       单位-->

     </el-row>

    <el-form-item>
      <el-button type="primary" @click="sumbmit">添加</el-button>
      <el-button @click="canceAdd">取消</el-button>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
import axios from "axios";

export default {
  name: "addgoods",
  data(){
    return {
      form:{
        goodsName:"",
        goodsCostPrice:'',
        goodsSalesPrice:'',
        isPiece:'',
        isWeight:'',
        weight:null,
        unit:'',
        salesType:"1",
        goodsType:null,
        status:true,
        supplier:null,
        weightUnit:''
      },
      goodsTypeList:[],
      supplierList:[],
      units: [{
        value: '部',
        label: '部'
      }, {
        value: '件',
        label: '件'
      }, {
        value: '个',
        label: '个'
      }
        ,{
          value: '包',
          label: '包'
        },
        {
          value: '箱',
          label: '箱'
        },
        {
          value: '瓶',
          label: '瓶'
        }
      ],
      weights:[
        {
          value:'克',
          label:'克',
        },
        {
          value: '千克',
          label: "千克"
        }
      ]
    }
  },
  mounted() {
    //加载商品分类信息
    axios.get("http://localhost:8080/api/classify/findAll")
    .then((resp)=>{
      if (resp.data.code==200){
        this.$message({
          message:resp.data.msg,
          type:'success'
        })

        let data = resp.data.data;
        for (let i = 0; i < data.length; i++) {
          let obj ={
            label:data[i].classifyName ,
            value:data[i].id
          }
          this.goodsTypeList.push(obj)
        }
      }
    })

    //加载供应商信息
    axios.get('http://localhost:8080/api/supplier/findAll')
    .then((resp)=>{
      //查找所有供应商信息
      let data = resp.data.data;
      console.info(data)
      for (let i = 0; i < data.length; i++) {
        let obj ={
          label:data[i].supplierName,
          value: data[i].id
        }
        //将供应商信息添加到数组中
        this.supplierList.push(obj)
      }
    })
  },
  methods:{
    //添加商品提交数据
    sumbmit(){
      //非空判断
      if (this.form.goodsName==null||this.form.goodsName==''){
        this.$message({
          message:"货品名称不可以为空",
          type:'error'
        })
        return false;
      }
      else if (this.form.goodsCostPrice==null){
        this.$message({
          message:'货品成本价格不可以为空',
          type:'error'
        })
        return  false;
      }

      else if (this.form.goodsSalesPrice==null){
        this.$message({
          message:'货品销售价格不可以为空',
          type:'error'
        })
        return  false;
      }

      else if (this.form.unit==null){
        this.$message({
          message:'货品单位不可以为空',
          type:'error'
        })
        return  false;
      }
      //处理数据
      if (this.form.status){
        this.form.status=0;
      }
      else this.form.status=1;
      if (this.form.salesType==0){
        this.form.isPiece=0;
        this.form.isWeight=1;
      }
      else {
        this.form.isWeight=0;
        this.form.isPiece=1;
      }
      //发送请求添加商品
      axios({
        method:'post',
        url:'http://localhost:8080/api/goods/add',
        data:this.form
      }).then(resp=>{
        if (resp.data.code==200){
          this.$message({
            message:resp.data.msg,
            type:'success'
          })
          //添加成功关闭对话框
          this.$bus.$emit("closeAddGoods",true)
        }
        else this.$message({
          message:resp.data.msg,
          type:'error'
        })

      })

    },
    //取消添加商品信息
    canceAdd(){
      this.$bus.$emit('closeAddGoods',false)
    }


  }
}
</script>

<style scoped>

</style>